package com.lagou.webwork.servlet.classes; /**
 * @author sjp
 * @date 2021/6/7 15:58
 * @description
 */

import com.lagou.webwork.model.Class;
import com.lagou.webwork.model.ListResult;
import com.lagou.webwork.model.Result;
import com.lagou.webwork.model.Student;
import com.lagou.webwork.service.ClassService;
import com.lagou.webwork.service.StudentService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet(name = "GetClassModelServlet", value = "/getClassModel")
public class GetClassModelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService service = new ClassService();
        int id = Integer.valueOf(request.getParameter("id")) ;

        ListResult<Class> classList = service.getClassModelById(id);

        Result<Class> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (classList.getTotalCount()>0){
            result.setDataList(classList.getDataList());
            result.setTotalCount(1);
            result.setMsg("获取列表信息成功!");
        }else{
            result.setMsg("获取列表失败，请刷新后再试。");
        }

        //设置编码格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //输出给前端
        PrintWriter writer = response.getWriter();
        writer.print(result.toString());
        writer.flush();
    }
}
